﻿using System;
using System.Collections.Generic;
using System.Linq;
using Backend.JYD.Model;
using Backend.JYD.Model.Task;

namespace Backend.JYD.EFDAL.DAL
{
    public class TaskStepModelDAL : Backend.JYD.IDAL.Task.ITaskStepModel
	{
		#region 查询数据TaskStepModel

		/// <summary>
		/// 查询数据
		/// </summary>
		public PageInfo<TaskStepModel> GetTaskStepModelByPage(PageInfo page)
		{
			using (JydDataContext db = new JydDataContext())
			{
				IQueryable<TaskStepModel> query = db.TaskStepModels.Where(p=> p.Status > 0);
				//其他条件代码
				return new PageInfo<TaskStepModel>(page, query);
			}
		}
		/// <summary>
		/// 查询数据
		/// </summary>
		public TaskStepModel GetTaskStepModelByTaskStepId(Int64 taskStepId)
		{
			using (JydDataContext db = new JydDataContext())
			{
                IQueryable<TaskStepModel> query = db.TaskStepModels.Where(p => p.TaskStepId == taskStepId);
				return query.FirstOrDefault();
			}
		}

        /// <summary>
        /// 查询数据
        /// </summary>
        public IList<TaskStepModel> GetTaskStepModelByTaskStepIds(IList<Int64> taskStepIds)
        {
            Int64[] ids = taskStepIds.ToArray();
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<TaskStepModel> query = db.TaskStepModels.Where(p => ids.Contains(p.TaskStepId));
                return query.ToList();
            }
        }


        public IList<TaskStepModel> GetAllTaskStepModel()
        {
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<TaskStepModel> query = db.TaskStepModels;
                return query.ToList();
            }
        }


        public IList<TaskStepModel> GetTaskStepModelByTaskModeId(Int64 taskModeId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<TaskStepModel> query = db.TaskStepModels.Where(p => p.TaskModeId == taskModeId).OrderBy(p=>p.StepId);
                return query == null ? null : query.ToList();
            }
        }

        public TaskStepModel GetTaskStepModelByTaskModeIdAndStepId(Int64 taskModeId, int stepId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<TaskStepModel> query = db.TaskStepModels.Where(p => p.TaskModeId == taskModeId && p.StepId == stepId);
                return query.FirstOrDefault();
            }
        }

		#endregion

		#region 插入数据

		/// <summary>
		/// 插入数据TaskStepModel
		/// </summary>
		public Int64 AddTaskStepModel(TaskStepModel info)
		{
			using (JydDataContext db = new JydDataContext())
			{
				db.TaskStepModels.Add(info);
				db.SaveChanges();
				return info.TaskStepId;
			}
		}
		#endregion

		#region 修改数据TaskStepModel

		/// <summary>
		/// 根据字段（TaskStepId）修改数据TaskStepModel
		/// </summary>
		public bool UpdateTaskStepModel(TaskStepModel info)
		{
			using (JydDataContext db = new JydDataContext())
			{
                var query = db.TaskStepModels.Where(p => p.TaskStepId == info.TaskStepId);
				if (query != null)
				{
					var temp = query.FirstOrDefault();
					if (temp != null)
					{
						temp.OpId = info.OpId;
						temp.UpdateTime = DateTime.Now;
						//.......
						return db.SaveChanges() > 0;
					}
				}
				return false;
			}
		}
		#endregion

	}
}